package com.lyubia.shop.manager.service.impl;

import com.lyubia.shop.common.exception.ParamsException;
import com.lyubia.shop.common.result.BaseResult;
import com.lyubia.shop.common.utils.AssertUtil;
import com.lyubia.shop.manager.mapper.GoodsImagesMapper;
import com.lyubia.shop.manager.pojo.GoodsImages;
import com.lyubia.shop.manager.service.GoodsImagesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class GoodsImagesServiceImpl implements GoodsImagesService {

    @Resource
    private GoodsImagesMapper goodsImagesMapper;


    /**
     * 保存商品图片信息
     * @param goodsImages
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public BaseResult saveGoodsImages(GoodsImages goodsImages) {
        checkGoodsImagesParam(goodsImages.getGoodsId(), goodsImages.getImageUrl());
        int i = goodsImagesMapper.insertSelective(goodsImages);
        return i > 0 ? BaseResult.success("图片保存成功!") : BaseResult.error("图片保存失败!");
    }

    /**
     * 校验商品图片参数
     * @param goodsId 商品id
     * @param imageUrl 图片地址
     */
    private void checkGoodsImagesParam(Integer goodsId, String imageUrl) {
        AssertUtil.isTrue(goodsId == null, "商品Id不能为空!");
        AssertUtil.isTrue(StringUtils.isBlank(imageUrl), "图片地址不能为空!");
    }
}
